<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>添加账单</title>
  <script src="./jquery-3.0.0.min.js"></script>
  <script src="./confing.js"></script>

</head>

<body>
  <form id="formName">
    事项<input type="text" name="name"></br>
    发生时间<input type="time" name="time"></br>
    支出类型
    <select name="type">
      <option value="1">支出</option>
      <option value="2">收入</option>
    </select></br>
    金额<input type="number" name="money"></br>
    备注<input type="textarea" name="bark"></br>
  </form>
  <button id="submit">提交</button>
  <button onClick="goList()">取消</button>
  <script>
    function goList() {
      window.location.href = "./index.html";
    }
    function goLogin() {
      window.location.href = "./login.html";
    }
    $(function () {
      console.log('页面初始化')

      $('#submit').bind('click', () => {
        let value = $('#formName').serializeArray();
        let data = {};
        $.each(value, function (index, item) {
          data[item.name] = item.value;
        });;
        console.log(data)
        // console.log('提交数据')
        $.ajax({
          url: DBHOST+"/account/create",
          type: "POST",
          headers: {
            'Token': localStorage.getItem('token')
          },
          data: data,
          success: function (result) {
            console.log(result);
            alert("添加成功");
            goList();
          },
          error: function (err) {
            let { code, msg } = err.responseJSON;
            if (code == 999) {
              alert(msg);
              goLogin();
            }
          }
        });
      })
    })

  </script>
</body>

</html>